<?php

namespace app\api\controller;

use app\admin\model\Povertypolicy as PovertypolicyMdl;
use app\admin\model\Povertypolicyledger;
use app\admin\model\Basicdata;
use app\admin\model\Member;
use app\admin\model\Image;

use think\Db;
use think\Exception;
use think\Request;

class Povertypolicy extends BaseController
{

    /**
     * 政策台账列表
     */
    public function getList()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId 不能为空');
        }
        if (empty($param->year)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, '年份(year) 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $where = [
            'townId' => $param->townId,
            'year' => $param->year
        ];
        if (isset($param->villageId) && $param->villageId) {
            //$where['villageId'] = $param->villageId;
        }

        $model = new Povertypolicyledger();
        $fields = 'id, cityId, xianId, townId, villageId';
        $list = $model->where($where)->field($fields)->order('createDate DESC')->limit($start, $length)->select();
        $total = $model->where($where)->count();

        if (empty($list)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到政策台账');
        }

        foreach ($list as &$item) {
            // 所在地
            $item['area'] = getArea($item, '');
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list, $total);
    }

    /**
     * 详情
     */
    public function getDetail()
    {
        $param = self::getHttpParam();
        $start = 0;
        $length = 20;

        if (empty($param->townId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'townId 不能为空');
        }
        if (empty($param->ledgerId)) {
            return show(config('status.ERROR_STATUS'), self::NOT_PARAM, 'ledgerId 不能为空');
        }
        if (!empty($param->start)) {
            $start = $param->start;
        }
        if (!empty($param->length)) {
            $length = $param->length;
        }

        $ledgerId = $param->ledgerId;
        $where = [
            'townId' => $param->townId,
            'policyledgerId' => $ledgerId
        ];

        $model = new PovertypolicyMdl();
        $fields = '*';
        $list = $model->where($where)->field($fields)->order('createDate DESC')->limit($start, $length)->select();
        $total = $model->where($where)->count();

        if (empty($list)) {
            return show(config('status.ERROR_STATUS'), self::NOT_DATA, '未查到政策台账明细');
        }

        $policyledgerInfo = Povertypolicyledger::where(['id' => $ledgerId])->find();
        foreach ($list as &$item) {
            $policyList = json_decode($item['policy'], true);
            $policys = [];
            $policyTotal = 0;
            foreach ($policyList as $policy) {
                foreach ($policy['policySpec'] as $policySpec) {
                    $policyName = show_policys($policy['policyType']);
                    if ($policySpec['specType'] != 0) {
                        $policyName .= $policyledgerInfo->year . '年';
                        if($policySpec['specType'] == 1){
                            $policyName .= show_quarter($policySpec['quarter']);
                        }else{
                            $policyName .= show_month($policySpec['month']);
                        }
                    }

                    $policys[] = [
                        'policyType' => $policySpec['explain'],
                        'policyName' => $policyName,
                        'policyValue' => $policySpec['explainValue']
                    ];

                    if($policySpec['explain'] == 1){
                        $policyTotal += $policySpec['explainValue'];
                    }
                }
            }
            // 户主
            $item['householderName'] = Member::where('id', $item['householderId'])->value('name');
            $item['year'] = $policyledgerInfo->year;
            $item['policy'] = $policys;
            $item['policyTotal'] = $policyTotal;
        }

        return showTotal(config('status.SUCCESS_STATUS'), self::MSG_SUCCESS, $list, $total);
    }
}